package com.novel.read.data.db.entity

import com.google.gson.annotations.SerializedName
import com.novel.read.constant.AppPattern
import com.novel.read.constant.BookType
import com.novel.read.utils.MD5Utils
import org.litepal.crud.LitePalSupport
import kotlin.math.max

data class Book(
    val authorPenname: String,                  //作者
    val bookId: Long,                           //书籍id
    val bookName: String,                       //书名
    val bookStatus: String,                     //书的状态，
    val categoryName: String?,                  //分类名
    val channelName: String?,                   //渠道名
    @SerializedName("className")
    val cName: String?,                         //分级名
    val coverImageUrl: String,                  //封面url
    val introduction: String,                   //简介
    val keyWord: String,                        //关键词
    var lastUpdateChapterDate: String,          //书籍最后更新时间
    val status: Int,                            //状态
    val wordCount: Long,                        //字数
    var totalChapterNum: Int = 0,               // 书籍目录总数
    var durChapterTitle: String? = null,        // 当前章节名称
    var durChapterIndex: Int = 0,               // 当前章节索引
    var durChapterPos: Int = 0,                 // 当前阅读的进度(首行字符的索引位置)
    var durChapterTime: Long = System.currentTimeMillis(),               // 最近一次阅读书籍的时间(打开正文的时间)
    var origin: String = BookType.net,          //源，本地书籍/网络书籍
    var originName: String = "",                //源名称
    var bookTypeId: Int = 0                     //书分类id
) : LitePalSupport() {

    fun isLocalBook(): Boolean {
        return origin == BookType.local
    }

    /****
     * 根据书名和封面图片url，得到一个固定的存储目录名
     */
    fun getFolderName(): String {
        return bookName.replace(AppPattern.fileNameRegex, "") + MD5Utils.md5Encode16(coverImageUrl)
    }

    fun isEpub(): Boolean {
        return originName.endsWith(".epub", true)
    }

    fun canUpdate(): Boolean {
        return bookStatus == BookType.serial
    }

    fun getUnreadChapterNum() = max(totalChapterNum - durChapterIndex - 1, 0)
}